<?php
/**
 * Created by PhpStorm.
 * User: xuke
 * Date: 16/7/1
 * Time: 下午3:52
 */

namespace App\Logic;
use App\Http\Model\GoodsSku AS GoodsSkuModel;
use Exception;
use Cache;

class GoodsSkuLogic extends  BaseLogic
{
    protected $_sku = null;

    public function __construct($sku)
    {
        $this->_sku = $sku;
    }

    public static function initBySku($sku, $isCache=1)
    {
        if (is_numeric($sku))
        {
            $cacheKey = "goods_sku_".$sku;
            if($isCache && !Cache::get($cacheKey)){
                $sku = GoodsSkuModel::find($sku);
                if (!$sku)
                {
                    throw new Exception('商品sku不存在', -2);
                }
                Cache::add($cacheKey, $sku,60*24*7);
            }else{
                $sku = Cache::get($cacheKey);
            }
        }else if (!$sku instanceof GoodsSkuModel)
        {
            throw new Exception('商品sku参数错误', -2);
        }
        return new static($sku);
    }

    public function getSku()
    {
        return $this->_sku;
    }
}